define(function (require, exports, module) {
    var $ = require("jquery");
    require("pagination");
    var util = require("util");
    var ui = require("ui");
    var angular = require("angular");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀
    var moment = require('moment');
    var print = require('plugins/print');

    var raiseFinanceOrdersModule = angular.module('raiseFinanceOrdersModule', []);

    function raiseFinanceOrderService($http, $q, $location) {
        var service = {
            orders: [],//orders列表

            initData: function () {
                if (service.filter) {
                    service.query(service.filter.pageIndex, service.filter.pageSize, service.filter.buildingId,
                        service.filter.status,  service.filter.period, service.filter.keyword);
                }
            },
            // 初始化查询表单数据
            initEditOrderDialog: function ($data, $dialog, $dialogScope, order) {
                if (order) {
                    angular.extend($data, {order: angular.extend({}, order), model: ((order.status == 1 || (order.status==2 && order.sub_status==0)) ? 0 : 1)});
                }
            },
            solutionsTimer:null,
            //获取解筹状态
            getSolutionsStatus:function(){
                var url = api_prefix + "/rclc/teambuy-order/get-batch-jie-chou-status";
                util.myAjax({
                    url: url,
                    type: "GET",
                    data: {buildingId: service.filter.buildingId},
                    context: this,
                    success: function (json) {
                        if (json.retCode == 0) {
                            var data= json.data;
                            if($("#content\\:jieshou_status").length==0){
                                util.showDialog({
                                    id: "jieshou_status",
                                    title: "提示",
                                    fixed: true,
                                    content: "待解筹"+data.total+"个，成功解筹"+data.success_count+"个。",
                                    width: 200,
                                    okValue: "解筹中...",
                                    cancelDisplay: false,
                                    ok: function () {
                                        if($("#content\\:jieshou_status").closest(".ui-dialog-grid").find(".ui-dialog-autofocus").text()!="确认"){
                                            return false;
                                        }
                                        return true;
                                    },
                                    cancel : function(){

                                    }
                                });
                                $("#content\\:jieshou_status").closest(".ui-dialog-grid").find(".ui-dialog-close").hide();
                            }
                            else{
                                $("#content\\:jieshou_status").html("待解筹"+data.total+"个，成功解筹"+data.success_count+"个。");
                            }
                            if(data.is_completed==0){
                                if(service.solutionsTimer==null){
                                    service.solutionsTimer=setInterval(function(){
                                        service.getSolutionsStatus();
                                    },1000);
                                }
                            }
                            else{
                                clearInterval(service.solutionsTimer);
                                service.solutionsTimer=null;
                                service.initData();
                                $("#content\\:jieshou_status").closest(".ui-dialog-grid").find(".ui-dialog-autofocus").text("确认");
                            }
                        } else {
                            ui.showErrTip(json.errMsg);
                        }
                    }
                });
            },
            //全部解筹
            allSolutions:function(){
                util.confirm('确定全部解冻认筹订单?', {
                    ok: function () {
                        var data = {buildingId: service.filter.buildingId};
                        var url = api_prefix + "/rclc/teambuy-order/batch-jie-chou";
                        util.myAjax({
                            url: url,
                            type: "POST",
                            data: data,
                            context: this,
                            success: function (json) {
                                if (json.retCode == 0) {
                                    service.getSolutionsStatus();
                                } else {
                                    ui.showErrTip(json.errMsg);
                                }
                            }
                        });
                    }
                });
            },
            /**
             * 打印条款初始化
             * @param $data
             * @param $dialog
             * @param $dialogScope
             * @param order
             */
            initPrintDialog: function ($data, $dialog, $dialogScope, order) {
                if (order) {
                    $http.post(api_prefix + '/rclc/teambuy/get-confirm-print', {teambuy_id: order.teambuy_id})
                        .then(function (res) {
                            var retCode = res.data.retCode;
                            if (retCode == 0) {
                                var data = res.data.data;
                                $data.orgName = data.orgName;
                                $data.confirm_content = data.confirm_content;
                                $data.confirm_name = data.confirm_name;
                            } else {
                                ui.showErrTip(res.data.errMsg);
                                return false;
                            }
                        });
                    angular.extend($data, {order: angular.extend({}, order), model: (order.status == 1 ? 0 : 1)});
                }
            },

            printClause: function () {
                print($('#printContent').html());
                return false;
            },
            /**
             * 查询认筹单
             * @param pageIndex
             * @param pageSize
             * @param buildingId
             * @param status 认筹单状态： 0：待付款 1：待解筹 2：已解筹
             * @param period
             * @param keyword
             */
            query: function (pageIndex, pageSize, buildingId, status, tradeStatus, period, keyword) {
                service.filter = {
                    pageIndex: pageIndex,
                    pageSize: pageSize,
                    buildingId: buildingId,
                    status: status,
                    tradeStatus: tradeStatus,
                    cycle: period,
                    keyword: keyword
                };
                $http.get(api_prefix + '/rclc/teambuy-order/get-teambuy-order-list', {params: service.filter})
                    .then(function (res) {
                        var retCode = res.data.retCode;
                        if (retCode == 0) {
                            var data = res.data.data;
                            service.orders = data.items;
                            service.total = data.total;
                            service.filter.pageIndex = pageIndex;
                        } else {
                            ui.showErrTip(res.data.errMsg);
                            return false;
                        }
                    });
            },
            updateTradeStatus: function ($data, $dialog) {
                var data = {
                    id: $data.order.teambuy_order_id,
                    result: $data.order.sub_status,
                    remark: $data.order.jiechou_content
                };

                var url = api_prefix + "/rclc/teambuy-order/update-trade-status";

                util.myAjax({
                    url: url,
                    type: "POST",
                    data: data,
                    context: this,
                    success: function (json) {
                        if (json.retCode == 0) {
                            service.initData();
                            ui.showSucTip();
                            $dialog.close();
                        } else {
                            ui.showErrTip(json.errMsg);
                        }
                    }
                });

                return false;
            },
            save: function ($data, $dialog) {
                var data = {
                    id: $data.order.teambuy_order_id,
                    result: $data.order.sub_status,
                    remark: $data.order.jiechou_content
                };

                var url = api_prefix + "/rclc/teambuy-order/jie-chou";

                util.myAjax({
                    url: url,
                    type: "POST",
                    data: data,
                    context: this,
                    success: function (json) {
                        if (json.retCode == 0) {
                            service.initData();
                            ui.showSucTip();
                            $dialog.close();
                        } else {
                            ui.showErrTip(json.errMsg);
                        }
                    }
                });

                return false;
            }
        };

        return service;
    }

    raiseFinanceOrdersModule.factory('raiseFinanceOrders', ['$http', '$q', '$location', raiseFinanceOrderService]);

    module.exports = raiseFinanceOrdersModule;
})
;